#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

void printCapacity(const vector<int> &con)
{
    //vector扩容原理
    //当vector中元素的个数与空间的大小相同的时候，size()==capacity()
    //会按照2 * size()进行扩容，然后将老的空间中的元素拷贝到新的空间
    //然后再将老的空间去进行回收（包括元素与内存都需要清理），然后
    //再将新的元素插入到新的空间的后面
    cout << "con.size() = " << con.size() << endl
         << "con.capacity() = " << con.capacity() << endl;
}

void test()
{
    vector<int> vec;
    vec.reserve(20);
    printCapacity(vec);

    cout << endl;
    vec.push_back(1);
    printCapacity(vec);

    cout << endl;
    vec.push_back(2);
    printCapacity(vec);

    cout << endl;
    vec.push_back(3);
    printCapacity(vec);

    cout << endl;
    vec.push_back(4);
    printCapacity(vec);

    cout << endl;
    vec.push_back(5);
    printCapacity(vec);

    cout << endl;
    vec.push_back(6);
    printCapacity(vec);

    cout << endl;
    vec.push_back(7);
    printCapacity(vec);

    cout << endl;
    vec.push_back(5);
    printCapacity(vec);

    cout << endl;
    vec.push_back(5);
    printCapacity(vec);
}

int main(int argc, char **argv)
{
    test();
    return 0;
}

